package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.LineItem;
import csu.web.mypetstore.persistence.DBUtil;
import csu.web.mypetstore.persistence.LineItemDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.out;

public class LineItemDaoImpl implements LineItemDao {
    private static final String getLineItemsByOrderId ="SELECT ORDERID, LINENUM AS lineNumber, ITEMID, QUANTITY, " +
            "UNITPRICE FROM LINEITEM WHERE ORDERID = ?";
    private static final String insertLineItem = "INSERT INTO LINEITEM (ORDERID, LINENUM, ITEMID, QUANTITY, UNITPRICE) " +
            "VALUES (?,?,?,?,?)";


    @Override
    public List<LineItem> getLineItemsByOrderId(int orderId) {
        List<LineItem> lineItems = new ArrayList<>();
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(getLineItemsByOrderId);
            preparedStatement.setInt(1,orderId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                lineItems.add(resultSetToLineItem(resultSet));
            }

            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
            DBUtil.closeResultSet(resultSet);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return lineItems;
    }

    @Override
    public void insertLineItem(LineItem lineItem) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(insertLineItem);
            preparedStatement.setInt(1,lineItem.getOrderId());
            preparedStatement.setInt(2,lineItem.getLineNumber());
            preparedStatement.setString(3,lineItem.getItemId());
            preparedStatement.setInt(4,lineItem.getQuantity());
            preparedStatement.setBigDecimal(5,lineItem.getUnitPrice());

            int isSucceed = preparedStatement.executeUpdate();
            if(isSucceed>0){
                out.println("inserLineItem!");
            }else{
                out.println("inserFalse!");
            }

            DBUtil.closeConnection(connection);
            DBUtil.closeStatement(preparedStatement);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private LineItem resultSetToLineItem(ResultSet resultSet){
        LineItem lineItem = new LineItem();
        try {
            lineItem.setLineNumber(resultSet.getInt("lineNumber"));
            lineItem.setItemId(resultSet.getString("itemId"));
            lineItem.setQuantity(resultSet.getInt("quantity"));
            lineItem.setUnitPrice(resultSet.getBigDecimal("unitPrice"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return lineItem;
    }
}
